package com.epalmpay.util.code;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.google.zxing.WriterException;
import sun.misc.BASE64Encoder;

public class CodeUtil {
	
	/**
	 * 生成二维码  带条形码
	 * @param stream
	 * @param params
	 */
	public static void encode(OutputStream stream,QRCodeParams params){
		try {
			BufferedImage barCode=BarCode.encode(params.getTxt(),  params.getWidth(),  params.getHeight() /4, params.getOffset());
			
			BufferedImage qrcode=QRCode.encode(params);
			BufferedImage image=addLogo_QRCode(qrcode,params.getLogoPath(),params.getLogoConfig()); 
			BufferedImage imageBox = new BufferedImage(image.getWidth()+20, image.getHeight()+barCode.getHeight()+50, BufferedImage.TYPE_INT_RGB);
	        
			
			//合成
			Graphics2D g = imageBox.createGraphics(); 
			g.setBackground(java.awt.Color.WHITE);     
	        g.clearRect(0, 0, imageBox.getWidth(), imageBox.getHeight()); 
	        g.drawImage(barCode, (imageBox.getWidth()-barCode.getWidth()) /2, 20, barCode.getWidth(), barCode.getHeight(), null);  
	        g.drawImage(image, 10, barCode.getHeight()+30, image.getWidth(), image.getHeight(), null);  
	        g.setColor(Color.BLACK);
	        //g.drawString(params.getTxt(), 10, 15);
	        double rate=3.20;  
	        int x=(int)((imageBox.getWidth()-10)/2-rate*g.getFontMetrics().stringWidth(params.getTxt())/2);  
	        MyDrawString(params.getTxt(), x+20, 15, rate, g);  
	        
	        
	        g.dispose();  
	        barCode.flush();  
	        image.flush();  
	        ImageIO.write(imageBox, "png", stream);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	} 
	
	/**
	 * 生成二维码  不带条形码
	 * @param stream
	 * @param params
	 */
	public static void code(OutputStream stream,QRCodeParams params){
		try {
			BufferedImage qrcode=QRCode.encode(params);
			BufferedImage image=addLogo_QRCode(qrcode,params.getLogoPath(),params.getLogoConfig()); 
			BufferedImage imageBox = new BufferedImage(image.getWidth()+20, image.getHeight()+50, BufferedImage.TYPE_INT_RGB);
			
			Graphics2D g = imageBox.createGraphics(); 
			g.setBackground(java.awt.Color.WHITE);     
	        g.clearRect(0, 0, imageBox.getWidth(), imageBox.getHeight()); 
	        g.drawImage(image, 10, 30, image.getWidth(), image.getHeight(), null);  
	        g.setColor(Color.BLACK);
	        
	        g.dispose();  
	        image.flush();  
	        ImageIO.write(imageBox, "png", stream);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 生成二维码  不带条形码
	 * 直接生成Base64二维码字节流字符串
	 * @param
	 * @param params
	 */
	public static String getBase64QRcodeStr(QRCodeParams params){
		String resultStr = "";
		try {
			BufferedImage qrcode=QRCode.encode(params);
			//BufferedImage image=addLogo_QRCode(qrcode,params.getLogoPath(),params.getLogoConfig());
			BufferedImage imageBox = new BufferedImage(qrcode.getWidth()+20, qrcode.getHeight()+50, BufferedImage.TYPE_INT_RGB);

			Graphics2D g = imageBox.createGraphics();
			g.setBackground(java.awt.Color.WHITE);
			g.clearRect(0, 0, imageBox.getWidth(), imageBox.getHeight());
			g.drawImage(qrcode, 10, 30, qrcode.getWidth(), qrcode.getHeight(), null);
			g.setColor(Color.BLACK);

			g.dispose();
			qrcode.flush();
			//ImageIO.write(imageBox, "png", stream);
			ByteArrayOutputStream os = new ByteArrayOutputStream();//新建流。
			ImageIO.write(imageBox, "png", os);//利用ImageIO类提供的write方法，将bi以png图片的数据模式写入流。
			byte b[] = os.toByteArray();//从流中获取数据数组。
			resultStr = new BASE64Encoder().encode(b);
			//System.out.println(resultStr);

		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultStr;
	}

	public static void MyDrawString(String str,int x,int y,double rate,Graphics2D g){  
        String tempStr=new String();  
        int orgStringWight=g.getFontMetrics().stringWidth(str);  
        int orgStringLength=str.length();  
        int tempx=x;  
        int tempy=y;  
        while(str.length()>0)  
        {  
            tempStr=str.substring(0, 1);  
            str=str.substring(1, str.length());  
            g.drawString(tempStr, tempx, tempy);  
            tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate);  
        }  
    }
	public static void encodeNotBar(OutputStream stream,QRCodeParams params){
		try {
			
			BufferedImage qrcode=QRCode.encode(params);
			
			BufferedImage image=addLogo_QRCode(qrcode,params.getLogoPath(),params.getLogoConfig()); 
			BufferedImage imageBox = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
			Graphics2D g = imageBox.createGraphics(); 
			g.setBackground(java.awt.Color.WHITE);     
	        g.clearRect(0, 0, imageBox.getWidth(), imageBox.getHeight()); 
	        g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);  
	        
	        g.dispose();    
	        image.flush();  
	        ImageIO.write(imageBox, "png", stream);
		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public static BufferedImage addLogo_QRCode(BufferedImage image, String logoPic, QRLogoConfig logoConfig)  
    {  
        try  
        {   
            BufferedImage logo = ImageIO.read(new File(logoPic));  
            System.out.println(logo); 
            int widthLogo = logo.getWidth(null)>image.getWidth()*2/10?(image.getWidth()*2/10):logo.getWidth(null),   
                heightLogo = logo.getHeight(null)>image.getHeight()*2/10?(image.getHeight()*2/10):logo.getHeight(null);  
            /** 
             * logo放在中心 
             */  
             int x = (image.getWidth() - widthLogo) / 2; 
             int y = (image.getHeight() - heightLogo) / 2;  

             
            BufferedImage imageBox = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            //开始绘制图片  
            Graphics2D g = imageBox.createGraphics(); 
            g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);  
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);  
            g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);  
            g.setStroke(new BasicStroke(logoConfig.getBorder()));  
            g.setColor(logoConfig.getBorderColor());  
            g.drawRect(x, y, widthLogo, heightLogo);  
               
            g.dispose();  
            logo.flush();  
            image.flush();  
            return imageBox;
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        } 
        return image;      
    }  
}
